package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.annotation.SystemControllerLog;
import com.jsb.service.AnswerService;
import com.jsb.service.DiscussService;
import com.jsb.service.QuestionService;
import com.jsb.service.UserService;
import com.jsb.common.config.Const;
import com.jsb.common.utils.BeanUtils;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.DiscussDto;
import com.jsb.dto.UserDiscuss;
import com.jsb.model.Answer;
import com.jsb.model.Discuss;
import com.jsb.model.DiscussExample;
import com.jsb.model.DiscussExample.Criteria;
import com.jsb.model.Question;
import com.jsb.model.User;

@Controller
public class DiscussController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/discuss/list.do";
	
	@Resource
	private DiscussService discussService;
	@Resource
	private UserService userService;
	@Resource
	private AnswerService answerService;
	@Resource
	private QuestionService questionService;
	
	@RequestMapping("/discuss/list")
	public String list(
			Model model,String type,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		DiscussExample discussExample = new DiscussExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phone);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			Criteria criteria = discussExample.createCriteria();
			if(StringUtils.isNotEmpty(phone)){
				criteria.andUserUuidLike("%"+userid+"%");
			}
			if(StringUtils.isNotEmpty(type)){
				criteria.andUserUuidEqualTo(type);
			}
			discussExample.or(criteria);
		}
		int totalCount = discussService.countDiscuss(discussExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		discussExample.setPageIndex((pageNum - 1) * pageSize);
		discussExample.setPageSize(pageSize);
		discussExample.setOrderByClause("date DESC");
		List<Discuss> discusses = discussService.getPageDiscuss(discussExample);
		List<DiscussDto> discusss = new ArrayList<DiscussDto>();
		//封装属性
		for (Discuss discuss : discusses) {
			String userId =  discuss.getUserUuid();
			User user = userService.getUserByPhoneNumber(userId);
			//进行封装
			DiscussDto dto = new DiscussDto();
			BeanUtils.copyProperties(dto, discuss);
			dto.setUser(user);
			discusss.add(dto);
		}
		
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("discusss", discusss);
		return "/Discuss/list";
	}
	
	@RequestMapping("/discuss/toAdd")
	public String toAdd(Model model) {
		return "/Discuss/add";
	}

	@RequestMapping("/discuss/add")
	public String add(Model model, @ModelAttribute("discuss") Discuss discuss) {
		discussService.addDiscuss(discuss);
		return redirect;
	}

	@RequestMapping("/discuss/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		discussService.delDiscussByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/discuss/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Discuss discus =  discussService.getDiscussByUuid(uuid);
		String userUuid = discus.getUserUuid();
		User user = userService.getUserByPhoneNumber(userUuid);
		//将用户信息封装到discuss中
		DiscussDto discuss = new DiscussDto();
		BeanUtils.copyProperties(discuss, discus);
		discuss.setUser(user);
		//将问题回答的信息封装到discuss中
		model.addAttribute("discuss", discuss);
		return "/Discuss/edit";
	}

	@RequestMapping("/discuss/edit")
	@SystemControllerLog(description = "编辑评论")
	public String edit(Model model, @ModelAttribute("discuss") Discuss discuss) {
		discussService.updateDiscuss(discuss);
		return redirect;
	}

	@RequestMapping("/discuss/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Discuss discuss = discussService.getDiscussByUuid(uuid);
		String userUuid = discuss.getUserUuid();
		User user = userService.getUserByPhoneNumber(userUuid);
		UserDiscuss userDiscuss= new UserDiscuss();
		//将用户信息封装到userDiscuss中
		userDiscuss.setUser(user);
		userDiscuss.setDiscuss(discuss);
		model.addAttribute("userDiscuss", userDiscuss);
		return "/Discuss/view";
	}
}
